<template>
  <div class="toolbar">
    <div class="toolbar__left">
      <div class="toolbar__zoom-percent">
        {{ parseInt(zoomSize.current * 100) }}%
      </div>

      <el-tooltip
        v-for="(item, index) in toolsBtn"
        :key="index"
        effect="dark"
        :content="item.title"
        placement="bottom"
      >
        <div
          class="toolbar__btn"
          :class="[item.classNam]"
          @click="item.handler"
        >
          <i :class="item.icon" />
        </div>
      </el-tooltip>
    </div>

    <div class="toolbar__right">
      <el-button
        type="primary"
        size="mini"
        @click="handleCreate"
      >
        {{ $route.query.id ? '修改' : '发布' }}
      </el-button>
    </div>
  </div>
</template>
<script>
import { ZOOM_OUT, ZOOM_IN } from '@/views/approvalFlow/js/graphLib'

export default {
  name: '',
  props: {
    mousewheelZoom: {
      type: Number,
      default: 1
    },
    historyBtnState: {
      type: Object,
      default: () => ({
        canRedo: false,
        canUndo: false
      })
    }
  },
  data() {
    return {
      zoomSize: {
        min: 0.1,
        max: 2.0,
        current: 1.0
      }
    }
  },
  computed: {
    zoom() {
      return {
        ZOOM_IN,
        ZOOM_OUT
      }
    },

    toolsBtn() {
      return [
        {
          title: '缩小画布',
          icon: 'el-icon-zoom-out',
          handler: () => this.handleZoom('reduce')
        },
        {
          title: '放大画布',
          icon: 'el-icon-zoom-in',
          handler: () => this.handleZoom('add')
        },
        {
          title: '撤销',
          icon: 'iconfont icon-undo',
          classNam: this.historyBtnState.canUndo ? '' : 'history--disabled',
          handler: () => this.handleHistory('undo')
        },
        {
          title: '恢复',
          icon: 'iconfont icon-redo',
          classNam: this.historyBtnState.canRedo ? '' : 'history--disabled',
          handler: () => this.handleHistory('redo')
        },
        {
          title: '清空画布',
          icon: 'el-icon-delete',
          handler: () => this.handleClear()
        },
        {
          title: '保存',
          icon: 'iconfont icon-save',
          handler: () => this.handleSave()
        }
      ]
    }
  },
  watch: {
    mousewheelZoom(n, o) {
      this.zoomSize.current = n
    }
  },
  methods: {
    handleZoom(zoom) {
      if (zoom === 'add') {
        if (this.isBigFUN(this.zoomSize.max, this.zoomSize.current)) {
          this.zoomSize.current += 0.1
        }
      } else {
        if (this.isBigFUN(this.zoomSize.current, this.zoomSize.min)) {
          this.zoomSize.current -= 0.1
        }
      }
      this.$emit('zoom', this.zoomSize.current)
    },

    handleSave() {
      this.$emit('save')
    },

    handleClear() {
      this.$emit('clear')
    },

    handleHistory(type) {
      if (
        type === 'undo' && !this.historyBtnState.canUndo ||
        type === 'redo' && !this.historyBtnState.canRedo
      ) return
      this.$emit('history', type)
    },

    isBigFUN(num1, num2) {
      return (num1 - num2) > 0.0001
    },

    handleCreate() {
      this.$emit('create')
    }
  }
}
</script>
<style lang="scss" scoped>
.toolbar {
  padding: 10px 20px;
  background-color: #fff;
  z-index: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;

  &__left {
    display: flex;
    align-items: center;
  }

  &__zoom-percent {
    min-width: 34px;
    margin-right: 10px;
    font-size: 14px;
    color: rgb(128, 128, 128);
  }

  &__btn {
    margin-right: 20px;
    cursor: pointer;
    color: #333;

    >i {
      font-size: 16px;
    }

    &:hover {
      color: #2F88FF;
    }

    &:last-child {
      margin-right: 0;
    }
  }

  .history--disabled {
    opacity: .5;
  }
}
</style>
